import { getUserRequest } from "@/apis/user";
import { makeAutoObservable } from "mobx";
import { makePersistable } from "mobx-persist-store";

export type userTypes = {
	id: number,
	username: string,
	avater: string,
	phone: string,
	name: string,
	sex: number,
	age: number,
	password: string,
	key: number
}

interface optionType {
	label: string,
	value: string | number | null;
	children?: optionType[]
}

export class userStore {
	users: userTypes[] = []
	userOptions: optionType[] = []
	constructor() {
		makeAutoObservable(this, {}, { autoBind: true })
		makePersistable(this, {
			name: 'usersStore',
			properties: ['users', 'userOptions'],
			storage: window.localStorage,
		})
	}

	setUsers(users: userTypes[]) {
		this.users = users
	}

	getUsers() {
		getUserRequest().then((re: any) => {
			re.result?.forEach((item: userTypes) => {
				item.key = item.id
			})
			this.setUsers(re.result)
			this.userOptions = re.result?.map((item: userTypes) => ({
				label: item.username,
				value: item.id
			}))
		})
	}

}